package com.how.warlock.player;

import java.awt.Graphics;

import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;

import com.how.warlock.Game;
import com.how.warlock.entities.Entity;
import com.how.warlock.entities.PlayerEntity;

public class Player {
	private boolean key_up,key_down,key_right,key_left,mouse_left,mouse_right;
	
	float maxSpeed = 100;
	float xSpeed,ySpeed;
	int velocity=5;
	float gravity = 1;
	float noise=0;
	float xNoise=200,yNoise=150;
	
	
	
	PlayerEntity entity;
	
	Game game;
	
	public Player(Game game){
		entity = new PlayerEntity(game, 300, 300);
		this.game = game;
	}
	
	public void update(long delta){
		control();
		
		
		
		entity.move(delta);
		
		Camera.update(entity.getX(), entity.getY(), game);
	}
	
	public void draw(){
		entity.draw();
	}
	
	private void control(){
		
		if(key_up && noise==0){
			if(ySpeed > -maxSpeed)ySpeed += -velocity;
		}else if(key_up && noise == 1){
			if(ySpeed >= -maxSpeed)ySpeed += -velocity;
		}
		
		if(key_down && noise==0){
			if(ySpeed < maxSpeed)ySpeed += velocity;
		}else if(key_down && noise == 1){
			if(ySpeed <= maxSpeed)ySpeed += velocity;
		}
		
		if(key_left && noise==0){
			if(xSpeed > -maxSpeed)xSpeed += -velocity;
		}else if(key_left && noise == 1){
			if(xSpeed >= -maxSpeed)xSpeed += -velocity;
		}
		
		if(key_right && noise==0){
			if(xSpeed < maxSpeed)xSpeed += velocity;
		}else if(key_right && noise == 1){
			if(xSpeed <= maxSpeed)xSpeed += velocity;
		}
		
		
		
		//Gravity
		if(xSpeed<0) xSpeed += gravity;
		if(xSpeed>0) xSpeed += -gravity;
		
		if(ySpeed<0) ySpeed += gravity;
		if(ySpeed>0) ySpeed += -gravity;
		
		//NOISE
		if (xSpeed > maxSpeed || xSpeed < -maxSpeed || ySpeed > maxSpeed || ySpeed < -maxSpeed){
			noise=1;
		}else noise=0;
		
		if(mouse_left){
			xSpeed=xNoise;
			ySpeed=yNoise;
		}
		
		if(key_down) entity.setDirection(0);
		if(key_up) entity.setDirection(1);
		if(key_right) entity.setDirection(2);
		if(key_left) entity.setDirection(3);
		
		entity.setMovement(xSpeed, ySpeed);
	}
	
	
	
	public void getControlData(){
		
		mouse_left = Mouse.isButtonDown(Control.MOUSE_LEFT);
		mouse_right = Mouse.isButtonDown(Control.MOUSE_RIGHT);
		
		while (Keyboard.next()){
			if (Keyboard.getEventKeyState()){
				if(Keyboard.getEventKey() == Control.KEY_UP)  key_up = true;
				if(Keyboard.getEventKey() == Control.KEY_DOWN)  key_down = true;
				if(Keyboard.getEventKey() == Control.KEY_RIGHT)  key_right = true;
				if(Keyboard.getEventKey() == Control.KEY_LEFT)  key_left = true;
			} else { 
				if(Keyboard.getEventKey() == Control.KEY_UP)  key_up = false;
				if(Keyboard.getEventKey() == Control.KEY_DOWN)  key_down = false;
				if(Keyboard.getEventKey() == Control.KEY_RIGHT)  key_right = false;
				if(Keyboard.getEventKey() == Control.KEY_LEFT)  key_left = false;
			}
		}
	}
}
